package com.j2eefast.flowable.bpm.controller;

import com.j2eefast.common.core.business.annotaion.BussinessLog;
import com.j2eefast.common.core.enums.BusinessType;
import com.j2eefast.common.core.utils.*;
import com.j2eefast.flowable.bpm.entity.BpmTaskFromEntity;
import com.j2eefast.flowable.bpm.entity.CompleteTaskEntity;
import com.j2eefast.flowable.bpm.service.BpmTaskFromService;
import com.j2eefast.flowable.bpm.service.IFlowableTaskService;
import com.j2eefast.flowable.bpm.utils.BpmConstant;
import com.j2eefast.framework.annotation.RepeatSubmit;
import com.j2eefast.common.core.controller.BaseController;
import com.j2eefast.framework.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.ui.ModelMap;

import java.util.HashMap;
import java.util.Map;
import java.util.Date;
import org.springframework.web.bind.annotation.*;
import com.j2eefast.flowable.bpm.entity.bpmCrmCarinfoEntity;
import com.j2eefast.flowable.bpm.service.bpmCrmCarinfoService;

/**
 * 车辆信息页面控制器
 * @author yhli
 * @date 2020-06-30 14:27
 */
@Controller
@RequestMapping("/bpm/carinfo")
public class bpmCrmCarinfoController extends BaseController{

    private String prefix = "modules/bpm/carinfo";
    @Autowired
    private bpmCrmCarinfoService bpmCrmCarinfoService;
    /**
    * 必须注入实例关联表单服务
    */
    @Autowired
    private BpmTaskFromService bpmTaskFromService;


    @Autowired
    private IFlowableTaskService flowableTaskService;


    @RequiresPermissions("bpm:carinfo:view")
    @GetMapping()
    public String carinfo(){
        return prefix + "/carinfo";
    }


    @GetMapping("/select/{taskId}/{id}")
    public String selectcarinfo(@PathVariable("taskId") String taskId,@PathVariable("id") String id, ModelMap mmp){
        mmp.put("taskId",taskId);
        mmp.put("id",id);
        return prefix + "/selectCar";
    }
        
    @RequestMapping("/list")
    @RequiresPermissions("bpm:carinfo:list")
    @ResponseBody
    public ResponseData list(@RequestParam Map<String, Object> params,bpmCrmCarinfoEntity bpmCrmCarinfoEntity) {
		PageUtil page = bpmCrmCarinfoService.findPage(params,bpmCrmCarinfoEntity);
		return success(page);
    }    
            
    @GetMapping("/add")
    public String add(ModelMap mmap){
         bpmCrmCarinfoEntity bpmCrmCarinfo = new bpmCrmCarinfoEntity();
          mmap.put("bpmCrmCarinfo", bpmCrmCarinfo);
        return prefix + "/add";
    }
        /**
         * 定义关联表单申请表单URL对应此处
         */
        @GetMapping("/add/{id}")
        public String add(@PathVariable("id") Long id, ModelMap mmp){
            //通过页面传入的表单ID查询表单关联信息
            BpmTaskFromEntity bpmTaskFrom = bpmTaskFromService.getTaskFromById(id);
            mmp.put("bpmTaskFrom", bpmTaskFrom);
            mmp.put("user", UserUtils.getUserInfo());
            return prefix + "/add";
        }

    /**
     * 新增
     */
    @RepeatSubmit
    @RequiresPermissions("bpm:carinfo:add")
    @BussinessLog(title = "车辆信息", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData addbpmCrmCarinfo(@Validated bpmCrmCarinfoEntity bpmCrmCarinfo,@Validated CompleteTaskEntity params){
        //校验参数
        ValidatorUtil.validateEntity(bpmCrmCarinfo);
        ResponseData result= bpmCrmCarinfoService.savebpmCrmCarinfo(bpmCrmCarinfo)? success(): error("新增失败!");
        Map<String,Object> mp =new HashMap<>();
        mp.put(BpmConstant.IS_WAI,params.getIsWai());
        flowableTaskService.complete(params);
        return result;
    }    
    
    /**
     * 修改
     */
    @RequiresPermissions("bpm:carinfo:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap){
        bpmCrmCarinfoEntity bpmCrmCarinfo = bpmCrmCarinfoService.getbpmCrmCarinfoById(id);
        mmap.put("bpmCrmCarinfo", bpmCrmCarinfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存车辆信息
     */
    @RepeatSubmit
    @RequiresPermissions("bpm:carinfo:edit")
    @BussinessLog(title = "车辆信息", businessType = BusinessType.UPDATE)
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData editbpmCrmCarinfo(bpmCrmCarinfoEntity bpmCrmCarinfo){
		ValidatorUtil.validateEntity(bpmCrmCarinfo);
        return bpmCrmCarinfoService.updatebpmCrmCarinfoById(bpmCrmCarinfo)? success(): error("修改失败!");
    }    

    /**
     * 删除
     */
    @RepeatSubmit
    @BussinessLog(title = "车辆信息", businessType = BusinessType.DELETE)
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @RequiresPermissions("bpm:carinfo:del")
    @ResponseBody
    public ResponseData del(String[] ids) {
      return bpmCrmCarinfoService.deletebpmCrmCarinfoByIds(ids)? success(): error("删除失败!");
    }    
}
